//*****************************************/
// Blance
// ShareWAF.com 负载均衡模块
// Auther: WangLiwen
//*****************************************/

//三方模块
var express = require("express")();
var body_parser = require("body-parser");

//端口
var port = 8090;

//存放目标
var {
    pools
} = require('./config');

const {
    healthCheck,
    intevalCheck
} = require('./health');

const {
    simulateServerStatus,
} = require('./util');

const {
    assign2server
} = require('./polling');

let workpools = healthCheck(pools);

//监听
express.listen(port);

//定时监听服务器状态
intevalCheck(() => {
    workpools = healthCheck(pools);
});

//模拟server掉线
simulateServerStatus(pools)

console.log("blance server at port:", port);

//body-parser
express.use(body_parser.urlencoded({extended: true}));

//注册
express.get("/test",function(req,res,next){
    res.send('ok');
});

//随机访问负载
express.use(function(req,res,next){
    //随机数
    const visitKey = assign2server(healthCheck(pools));
    console.log("blance visit: ", visitKey);
    next();
})
